<!--
 * @author guojiteng
 投票活动-预览
-->
<template>
  <div class="tphd-preview">
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :visible.sync="state"
      title="预览"
      width="750px"
      center
      :before-close="handleCancel"
    >
      <div v-loading="previewLoading" class="preview-container">
        <section v-if="showPage ==1" class="page-main">
          <div class="bgi">
            <img v-if="info.coverImgId" :src="view + info.coverImgId" alt>
          </div>
          <div class="content">
            <div class="info">
              <div class="topMenu">
                <div class="mitem introduce">
                  <span>活动介绍</span>
                </div>
                <div class="mitem myVote">
                  <span>我的投票</span>
                </div>
              </div>
              <div v-if="flag==false" class="time">
                距离活动结束：
                <span class="red">{{ time }}</span>
              </div>
              <div v-else class="time">活动已结束</div>
            </div>
            <div class="desc">
              <div class="tagging">
                <img src="../images/tagging.png" alt>
              </div>
              <span>
                1.每人每天可投次数：
                <span v-if="info.dailyLimit == -1">无上限</span>
                <span v-else>{{ info.dailyLimit }}次</span>， 最多可投次数：
                <span v-if="info.everybodyLimit == -1">无上限</span>
                <span v-else>{{ info.everybodyLimit || 0 }}次</span>
              </span>
              <div class="descM" />

              <span>
                2.每次最少选择：{{
                  info.minQuantity ? info.minQuantity + "项" : "不限"
                }}， 最多可选：{{
                  info.maxQuantity ? info.maxQuantity + "项" : "不限"
                }}
              </span>
              <div class="descM" />
              <span> 3.因参与人员数量较大，投票结果5分钟更新一次，记得来看呀~ </span>
            </div>
            <div class="nums">
              <div class="numItem">
                <div class="num">{{ info.votingCandidateNum || 0 }}</div>
                <div class="text">投票项</div>
              </div>
              <div v-if="info.isShowNumber == '1'" class="numItem">
                <div class="num">{{ info.votingSum || 0 }}</div>
                <div class="text">累计投票</div>
              </div>
              <div class="numItem">
                <div class="num">{{ info.pageview }}</div>
                <div class="text">访问量</div>
              </div>
            </div>
            <div class="menuTabs">
              <div class="tabItem">
                <span :class="{choose:activeName=='xh'}" @click="changeType('xh')">按序号</span>
              </div>
              <div class="tabItem">
                <span :class="{choose:activeName=='ps'}" @click="changeType('ps')">按票数</span>
              </div>
            </div>
            <div class="voteList">
              <div
                v-for="(item, i) in list"
                :key="i"
                class="vitem"
                :class="{ noRight: (i + 1) % 2 == 0 }"
                @click="toDetail(item)"
              >
                <div class="ione">
                  <div class="ioneText">
                    {{ item.serialNumber }}号
                    <span v-if="info.isShowNumber == '1'">
                      /
                      <span class="tp">{{ item.selectedNumber || 0 }}票</span>
                    </span>
                  </div>
                  <div class="ioneIcon">
                    <img class="img-icon" src="../images/inactiveIcon.png">
                  </div>
                </div>
                <div class="itwo">
                  <img :src="view + item.coverImgId" alt>
                </div>
                <div class="ithree">{{ item.mainTitle }}</div>
                <div v-if="item.briefIntroduction" class="ifour">{{ item.briefIntroduction || "" }}</div>
                <div v-if="item.withTitle" class="ifive">{{ item.withTitle }}</div>
              </div>
              <div class="lastItem" />
            </div>
          </div>
        </section>
        <div v-if="showPage ==2" class="page-main-second">
          <section class="header-content">
            <img class="back" src="../images/back-b.png" @click="showPage =1">
            <span class="title">详情</span>
          </section>
          <div class="content">

            <div v-if="detail.mainTitle" class="title">
              <span>{{ detail.mainTitle }}</span>
            </div>
            <div class="time">
              序号：{{ detail.serialNumber }}&nbsp;&nbsp;&nbsp;&nbsp;
              <span> 票数：{{ ps }}</span>
            </div>
            <div class="data">
              <template v-if="detail.videoList && detail.videoList.length > 0">
                <div v-for="item in detail.videoList" :key="item.attachmentId">
                  <video :src="view + item.attachmentId" controls />
                </div>
              </template>
              <template v-if="detail.picList && detail.picList.length > 0">
                <div v-for="item in detail.picList" :key="item.attachmentId">
                  <img :src="view + item.attachmentId" alt="">
                  <p class="explain">{{ item.explains }}</p>
                </div>
              </template>
              <p v-if="detail.text" class="news-text" v-html="detail.text" />
            </div>
            <div v-if="detail.fileList && detail.fileList.length" class="file">
              <div class="row1">
                <div class="left">附件</div>
              </div>
              <div v-for="i in detail.fileList" :key="i.attachmentId" class="fitem">
                <img
                  src=""
                  alt=""
                >
                <span>{{ i.attachmentName }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="handleCancel()">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import { getVotingInfo, pgetList, getOptionDetail } from '../api/index'

export default {
  name: 'Preview',
  directives: {
    elDragDialog
  },
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      state: true,
      previewLoading: false,
      view: '/API-DOCUMENT/document/file/download?id=',
      info: {},
      endTime: '2021-03-20 00:00:00',
      time: '',
      flag: false,
      activeName: 'xh',
      list: [],
      timer: null,
      nowCount: 0,
      showPage: 1, // 1是首页 2 是选项详情
      detail: {}// 选项详情
    }
  },
  watch: {
    activeName(val) {
      this.getList()
    }
  },
  mounted() {

  },
  created() {
    this.getData()
  },
  destroyed() {
    if (this.timer != null) {
      clearInterval(this.timer)
    }
  },
  methods: {
    changeType(t) {
      this.activeName = t
    },
    start() {
      this.timer = setInterval(() => {
        if (this.flag === true) {
          clearInterval(this.timer)
        }
        this.timeDown()
      }, 1000)
    },
    timeDown() {
      const endTime = new Date(this.info.endTime)
      const leftTime = parseInt((endTime.getTime() - this.nowCount) / 1000)
      this.nowCount += 1000
      const d = parseInt(leftTime / (24 * 60 * 60))
      const h = this.formate(parseInt(leftTime / (60 * 60) % 24))
      const m = this.formate(parseInt(leftTime / 60 % 60))
      const s = this.formate(parseInt(leftTime % 60))
      if (leftTime <= 0) {
        this.flag = true
      }
      this.time = `${d}天${h}小时${m}分${s}秒`
    },
    formate(time) {
      if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    },
    /**
     * 根编辑回显
     */
    getData() {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        getVotingInfo({ id: this.id }).then(res => {
          this.previewLoading = false
          this.info = res.data.data
          if (res.data.code === '200' && res.data.data) {
            this.nowCount = new Date(this.info.nowTime).getTime()
            this.start()
          }
          this.getList()
        }).catch(error => {
          this.previewLoading = false
          reject(error)
        })
      })
    },
    /**
     * 获取选项详情
     */
    toDetail(item) {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        getOptionDetail({ id: item.id }).then(res => {
          this.previewLoading = false
          if (res.data.code === '200' && res.data.data) {
            this.detail = res.data.data
            this.ps = item.selectedNumber || 0
            this.showPage = 2
          }
        }).catch(error => {
          this.previewLoading = false
          reject(error)
        })
      })
    },
    getList() {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        pgetList({ dataId: this.id, type: this.activeName }).then(res => {
          this.previewLoading = false
          this.list = res.data.data || []
          // console.log('list', this.list)
        }).catch(error => {
          this.previewLoading = false
          reject(error)
        })
      })
    },
    handleCancel() {
      this.$emit('closeSet')
    }
  }
}
</script>

<style lang="scss">
.tphd-preview {
  position: relative;

  .preview-container {
    height: 80vh;
    position: relative;

    .page-main {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      overflow: auto;
      -webkit-overflow-scrolling: touch;
      padding: 0 15px;

      .bgi {
        position: absolute;
        left: 0;
        right: 0;
        height: 330px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .content {
        padding: 300px 24px 0 24px;
        position: relative;

        .info {
          height: 262px;
          opacity: 1;
          background: #ffffff;
          border-radius: 16px;
          z-index: 999;

          .topMenu {
            height: 174px;
            padding: 22px 24px;
            display: flex;

            .mitem {
              height: 130px;
              flex: 1;
              font-size: 28px;
              font-family: PingFang SC Medium, PingFang SC Medium-Medium;
              font-weight: 500;
              text-align: left;
              color: #ffffff;
              line-height: 130px;
              padding-left: 30px;

              &.introduce {
                background: url("../images/introduce.png") no-repeat;
                background-size: 100% 100%;
              }

              &.myVote {
                margin-left: 22px;
                background: url("../images/myVote.png") no-repeat;
                background-size: 100% 100%;
              }
            }
          }

          .time {
            height: 88px;
            line-height: 88px;
            font-size: 26px;
            font-family: PingFang SC Medium, PingFang SC Medium-Medium;
            font-weight: 700;
            text-align: center;
            color: #333333;

            .red {
              @include font-color($mainColor);
            }
          }
        }

        .desc {
          min-height: 94px;
          opacity: 1;
          background: #ffead2;
          border-radius: 16px;
          margin: 20px 0;
          padding: 19px 10px 16px 50px;
          position: relative;
          font-size: 24px;
          font-family: PingFang SC Regular, PingFang SC Regular-Regular;
          font-weight: 400;
          text-align: left;
          color: #ca9f6f;

          .descM {
            margin-top: 8px;
          }

          .tagging {
            position: absolute;
            left: 15px;
            top: 23px;
            width: 24px;
            height: 24px;

            img {
              width: 24px;
              height: 24px;
            }
          }
        }

        .nums {
          height: 130px;
          opacity: 1;
          background: #ffffff;
          border-radius: 16px;
          display: flex;

          .numItem {
            flex: 1;
            text-align: center;
            padding: 24px 10px 28px 10px;

            .num {
              font-size: 28px;
              font-family: PingFang SC Medium, PingFang SC Medium-Medium;
              font-weight: 500;
              color: #333333;
            }

            .text {
              margin-top: 15px;
              font-size: 26px;
              font-family: PingFang SC Regular, PingFang SC Regular-Regular;
              font-weight: 400;
              color: #999999;
            }
          }
        }

        .menuTabs {
          height: 88px;
          display: flex;

          .tabItem {
            height: 88px;
            line-height: 88px;
            flex: 1;
            text-align: center;
            font-size: 26px;

            span {
              display: inline-block;
              color: #646566;
              cursor: pointer;

              &.choose {
                @include font-color($mainColor);
                width: 100px;
                position: relative;

                &::before {
                  content: "";
                  position: absolute;
                  height: 4px;
                  @include bg-color($mainColor);
                  bottom: 10px;
                  width: 100%;
                  left: 0;
                }
              }
            }
          }

          // background: #f8f8f8;
        }

        .voteList {
          margin-top: 20px;
          width: 100%;

          .vitem {
            padding: 20px;
            float: left;
            height: 460px;
            opacity: 1;
            background: #ffffff;
            border-radius: 12px;
            width: 49%;
            margin-bottom: 20px;
            margin-right: 2%;
            cursor: pointer;

            &.noRight {
              margin-right: 0;
            }

            .ione {
              height: 34px;
              display: flex;

              .ioneText {
                flex: 1;
                font-size: 26px;
                font-family: PingFang SC Regular, PingFang SC Regular-Regular;
                font-weight: 400;
                text-align: left;
                color: #333;

                .tp {
                  @include font-color($mainColor);
                }
              }

              .ioneIcon {
                height: 34px;
                height: 34px;

                .img-icon {
                  height: 34px;
                  height: 34px;
                }
              }
            }

            .itwo {
              margin-top: 20px;
              height: 210px;
              opacity: 1;
              background: #e9e9e9;

              img {
                width: 100%;
                height: 100%;
                border-radius: 12px;
              }
            }

            .ithree {
              margin-top: 19px;
              font-size: 26px;
              font-family: PingFang SC Medium, PingFang SC Medium-Medium;
              font-weight: 500;
              text-align: left;
              color: #333333;
            }

            .ifour {
              margin-top: 17px;
              font-size: 24px;
              font-family: PingFang SC Regular, PingFang SC Regular-Regular;
              font-weight: 400;
              text-align: left;
              color: #999999;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
              word-break: break-all;
            }

            .ifive {
              margin-top: 13px;
              font-size: 26px;
              font-family: PingFang SC Regular, PingFang SC Regular-Regular;
              font-weight: 400;
              text-align: left;
              color: #666666;
            }
          }

          .lastItem {
            clear: both;
          }
        }
      }
    }

    .page-main-second {
      width: 100%;
      height: 100%;
      overflow: hidden;
      position: relative;

      .header-content {
        background-color: #fff;
        padding: 0 15px;
        display: flex;
        align-items: center;
        position: relative;
        height: 44px;
        line-height: 44px;
        z-index: 3;

        .back {
          width: 11px;
          height: 19px;
          cursor: pointer;
        }

        .title {
          margin: 0 auto;
          color: #333;
          font-size: 17px;
        }
      }
      .content {
        padding: 20px 0;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        overflow-y: auto;
        .title {
          font-size: 20px;
          font-family: PingFang SC Medium, PingFang SC Medium-Medium;
          font-weight: 700;
          text-align: left;
          color: #333333;
        }
        .time {
          margin-top: 15px;
          margin-bottom: 15px;
          font-size: 12px;
          font-family: PingFang SC Regular, PingFang SC Regular-Regular;
          font-weight: 400;
          text-align: left;
          color: #999999;
        }
        .data {
          margin-top: 7px;
          img, video{
            width: 100%;
          }
          .explain{
            text-align: center;
          }
        }
        .file {
          margin: 12px 0;
          width: 100%;
          background: linear-gradient(#fff3ed 0%, #ffeadf 100%);
          border-radius: 16px;
          padding: 10px;
          padding-top: 0;
          .row1 {
            display: flex;
            height: 40px;
            font-family: PingFang SC Medium, PingFang SC Medium-Medium;
            line-height: 40px;
            border-bottom: 1px dotted #f5c7ac;
            .left {
              flex: 1;
              text-align: left;
              font-size: 14px;
              font-weight: 500;
              color: #333333;
            }
            .right {
              flex: 1;
              text-align: right;
              .btn {
                display: inline-block;
                width: 58px;
                text-align: center;
                height: 24px;
                border-radius: 12px;
                line-height: 24px;
                color: #fff;
                background: #eb523c;
              }
            }
          }
          .fitem {
            margin-top: 10px;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            word-break: break-all;
            img {
              width: 24px;
              height: 24px;
              vertical-align: middle;
            }
            span {
              margin-left: 10px;
              font-size: 12px;
              color: #666666;
            }
          }
        }
        .right-bottom {
          margin-top: 20px;
          height: 30px;
          font-family: PingFang SC Regular, PingFang SC Regular-Regular;
          font-weight: 400;
          text-align: left;
          color: #999999;
          .author {
            text-align: right;
            font-size: 13px;
          }
        }
        .news-text {
          font-size: 15px;
          video, audio, img {
            display: block;
            max-width: 100% !important;
            height: auto !important;
          }
        }
        .common-container {
          h4 {
            font-size: 14px;
            color: #333;
          }
          .comment-con {
            position: relative;
            min-height: 150px;
            .comment-item {
              display: flex;
              margin-bottom: 30px;
              &:last-child {
                margin-bottom: 10px;
              }

              & > img {
                width: 40px;
                height: 40px;
                border-radius: 50%;
                margin-right: 10px;
                flex: 0 0 auto;
              }

              .comment-item-right {
                .comment-item-right-content {
                  display: flex;
                  justify-content: space-between;
                  .comment-name {
                    font-size: 14px;
                    font-family: PingFangSC-Regular;
                    font-weight: bold;
                    color: rgba(51, 51, 51, 1);
                    line-height: 18px;
                  }
                }

                .comment-time {
                  display: flex;
                  align-items: center;
                  font-size: 12px;
                  font-family: PingFangSC-Regular;
                  font-weight: 400;
                  color: #999;
                  line-height: 18px;
                  .reply {
                    margin-left: 10px;
                    color: #666;
                  }
                }

                .comment-content {
                  width: 293px;
                  font-size: 14px;
                  font-family: PingFangSC-Regular;
                  font-weight: 400;
                  color: rgba(51, 51, 51, 1);
                  line-height: 21px;
                  margin-top: 5px;
                  word-wrap: break-word;
                }
                .comment-img {
                  display: flex;
                  margin: 5px 0;
                  .van-image {
                    margin-left: 5px;
                    &:first-child {
                      margin-left: 0;
                    }
                    img {
                      width: 82px;
                      height: 82px;
                      border-radius: 6px;
                    }
                  }
                }
                .reply-content {
                  width: 100%;
                  background-color: #fafafa;
                  padding: 5px 10px;
                  margin-top: 5px;
                  li {
                    margin-bottom: 20px;
                    position: relative;
                    &:last-child {
                      margin-bottom: 0;
                      &:after {
                        display: none;
                      }
                    }
                    &:after {
                      left: 0;
                      right: 0;
                      width: auto;
                      height: 1px;
                      position: absolute;
                      z-index: 0;
                      bottom: -10px;
                      content: "";
                      background-image: -webkit-gradient(
                              linear,
                              left bottom,
                              left top,
                              color-stop(50%, #ececec),
                              color-stop(0, transparent)
                      );
                      background-image: linear-gradient(0deg, #ececec 50%, transparent 0);
                    }
                  }
                  .reply-info {
                    display: flex;
                    align-items: center;
                    flex-wrap: wrap;
                    .replyer {
                      color: #9E9E9E;
                      span {
                        color: #666;
                      }
                    }
                    .reply-info {
                      color: #333;
                    }
                  }
                  .reply-img {
                    display: flex;
                    margin: 7px 0;
                    .van-image {
                      margin-left: 5px;
                      &:first-child {
                        margin-left: 0;
                      }
                      img {
                        width: 82px;
                        height: 82px;
                        border-radius: 6px;
                      }
                    }

                  }
                  .reply-time {
                    color: #999;
                    font-size: 12px;
                    margin-top: 4px;
                    .reply {
                      margin-left: 10px;
                      color: #666;
                    }
                  }
                  .get-son {
                    text-align: center;
                    @include font-color($mainColor)
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
